home *** CD-ROM | disk | FTP | other *** search
/ World of Education / World of Education.iso / world_s / startrek.zip / CHAPTER.008 < prev    next >
Text File  |  1989-05-15  |  46KB  |  793 lines

  1.         Chapter 8 PAGE 1 STARTREK THE COMPUTER PROGRAM
  2.  
  3.  
  4.                                     CHAPTER 8
  5.  
  6.         8.1  The TORpedo Command
  7.  
  8.              Now  that  the  ship can move around,  let  us  develop  the 
  9.         program for one of the weapon systems so that we can destroy  the 
  10.         enemy  as  we  travel  around the  Galaxy.   The  Photon  Torpedo 
  11.         function  is chosen first because the software uses many  similar 
  12.         functions to that of the Move function. It is in fact possible to 
  13.         combine the subroutines into one complex unwieldy routine if  you 
  14.         wish to save memory.  For such an example see Appendix  .
  15.  
  16.              Photon  Torpedoes are projectile anti-matter  weapons.   The 
  17.         Enterprise can carry ten of them at any one time.  They are fired 
  18.         at  the enemy in battle situations.  Should they enter  a  sector 
  19.         containing  a  Klingon, they will lock onto the  enemy  ship  and 
  20.         destroy it when they hit it.  A Klingon however does have a small 
  21.         probability   of  dodging  the  missile.   The   Federation   has 
  22.         superiority  in  Photon Torpedo technology so that if you  hit  a 
  23.         Klingon with one, you will always destroy it.  On the other  hand 
  24.         if a Klingon hits you with a torpedo, the shields will cope  with 
  25.         most  of  the blast and the sub-system closest to  the  point  of 
  26.         impact will suffer damage.
  27.  
  28.              It  is  also  possible that the torpedoes  will  also  enter 
  29.         sectors  containing starbases or stars.  If one hits a star,  the 
  30.         star  will absorb the torpedo.  If one hits a starbase,  in  real 
  31.         life the result would depend on whether the Base has its  shields 
  32.         up  or  not.  If the Starbase had its shields  up  nothing  would 
  33.         happen.  If not, some damage would occur including most  probably 
  34.         loss  of life.  In this simulation the Base will be destroyed  by 
  35.         the  torpedo,  a very improbable real life  occurrence.   However 
  36.         this is a training simulation one object of which is to teach the 
  37.         Starfleet  Academy  student  care in the  aiming  and  firing  of 
  38.         weapons.  The destruction of the Base by a Photon Torpedo is thus 
  39.         a  penalty  for  making a mistake.  If  the  student  happens  to 
  40.         destroy the only base in the simulation, (ie. the player destroys 
  41.         the only base in the game), too bad.
  42.  
  43.              The  Torpedo command function can be described by  the  flow 
  44.         chart  shown  in figure 8.1.  The PHOTON.TORPEDO  function  first 
  45.         tests  to  see  if the Torpedo sub-system is usable.   If  it  is 
  46.         damaged  a  message  is displayed to that effect.  If  it  is  in 
  47.         working  order, the software tests to see that there is at  least 
  48.         one  available to fire.  If none are left a message is  displayed 
  49.         to that effect.
  50.  
  51.              Assuming  that there is a torpedo available.   The  computer 
  52.         asks the player for the direction in which to fire it.  Once that 
  53.         is  established a loop begins which lasts as long as the  torpedo 
  54.         is in the quadrant and has not hit anything.
  55.  
  56.              The torpedo is moved one sector in the desired direction.  A 
  57.         test  is  then performed to see if the torpedo is  still  in  the 
  58.         quadrant.   If  it has left the quadrant, a "MISSED"  message  is 
  59.  
  60.  
  61.         Copyright (c) Joe Kasser 1989
  62.  
  63.  
  64.  
  65.  
  66.  
  67.         Chapter 8 PAGE 2 STARTREK THE COMPUTER PROGRAM
  68.  
  69.  
  70.         displayed  and the loop terminates.   If it is still  inside  the 
  71.         quadrant, the contents of the new sector are checked, because  if 
  72.         the  sector is occupied, the torpedo has hit something.   If  the 
  73.         sector is not occupied, the loop continues.
  74.  
  75.              Once  the torpedo enters a sector that is occupied  by  some 
  76.         other object, the routine tests to see which object.  If it is  a 
  77.         Klingon, and the Klingon is hit, it is destroyed.  The Klingon is 
  78.         deleted  from  the quadrant, the sector and the game  status.   A 
  79.         message is displayed that the enemy has been destroyed.  We  also 
  80.         allow  a  small  probability that the  Klingon  will  escape  the 
  81.         torpedo.   If this does happen, the sector is treated as a  blank 
  82.         as far as the passage of the torpedo is concerned.
  83.  
  84.              If  the torpedo enters a sector containing a star  and  hits 
  85.         it,  the star absorbs the torpedo.  If the torpedo does  not  hit 
  86.         the  star it is deflected due to the gravitational  field  around 
  87.         the  star.  In either event, a message is displayed  accordingly.  
  88.         If  the torpedo hits a starbase, it destroys it.  If the  torpedo 
  89.         returns  to  the sector containing the Enterprise, a  message  to 
  90.         that effect is displayed.
  91.  
  92.              The  BASIC language implementation of the flow  chart  could 
  93.         take  the format shown in figure 8.2.  The subroutine  begins  as 
  94.         usual  with a REMark in line 1000.  Line 1010 determines  if  the 
  95.         torpedo  tubes are working by testing the torpedo status  in  the 
  96.         damage control array.  The statements
  97.                'IF D(I) > 0 THEN PRINT "TUBES BLOCKED : GOTO 1210' 
  98.         first  test  the I'th element in the D(I) array  (Photon  Torpedo 
  99.         status).   If the contents of the element are greater than  zero, 
  100.         then  by  definition, some time is estimated to pass  before  the 
  101.         sub-system  will be operational.  In this case it must be due  to 
  102.         the fact that the Torpedo Control in damaged.  A "TUBES  BLOCKED" 
  103.         message  is accordingly displayed on the screen and  the  program 
  104.         counter  is advanced to the last line of the  subroutine,  namely 
  105.         line 1210. 
  106.  
  107.              Line  1020 next determines if there are any torpedoes  left.  
  108.         The Enterprise can only carry up to ten at a time.  At this time, 
  109.         we don't care how many are aboard so line 1020 just tests to  see 
  110.         if there are any at all.  The number of torpedoes on the ship  is 
  111.         stored  in the variable 'P'.  If the value of 'P' is  zero,  then 
  112.         there are none on board, and a message is displayed  accordingly.  
  113.         The last statement of the line then advances the program  counter 
  114.         to line 1210.
  115.  
  116.              The  dialogue to determine the desired course in  which  the 
  117.         torpedo is to be fired takes place in line 1300.  The statement 
  118.                           'INPUT "DIRECTION (1-9) ";C' 
  119.         both prompts the player to supply the direction and indicates the 
  120.         range  expected  for  the  reply.  This  is  another  example  of 
  121.         "friendly' prompting.  The reply is assigned to the variable 'C'.  
  122.         The second statement on the line 
  123.                             'IF C<1 OR C>9 THEN 1030' 
  124.         verifies that the input was within the desired range of less than 
  125.  
  126.  
  127.         Copyright (c) Joe Kasser 1989
  128.  
  129.  
  130.  
  131.  
  132.  
  133.         Chapter 8 PAGE 3 STARTREK THE COMPUTER PROGRAM
  134.  
  135.  
  136.         one  (<1)  or  greater than nine (>9).  If the value  of  'C'  is 
  137.         outside  that  range, the program line counter is reset  back  to 
  138.         line 1030 to phrase the question again.
  139.  
  140.              Line 1040 sets up the vertical and horizontal vectors of the 
  141.         direction  of  travel.  The row co-ordinate is  assigned  to  the 
  142.         variable  'Y1' and the column co-ordinate to 'X1' just as in  the 
  143.         MOVe operation.  This is done by the statements
  144.                               'Y1=S1+.5 : X1=S2+.5' 
  145.         which  set the co-ordinates of the sector containing the  torpedo 
  146.         equal  to that of the co-ordinates of the sector  containing  the 
  147.         Enterprise.  The numerical direction is converted to an angle  by 
  148.         the statement
  149.                                 'Y=(C-Z)*.785398' 
  150.         and  the row and column vectors of that direction are  determined 
  151.         by the statements
  152.                              'X=COS(Y) : Y=-SIN(Y)'.  
  153.         The  last item on the line displays the heading for the  tracking 
  154.         display.
  155.  
  156.              Line  1050  begins by decrementing the number  of  torpedoes 
  157.         aboard the ship using the statement 'P=P-Z'.  The loop that moves 
  158.         the  torpedo through the quadrant is then begun using a  FOR/NEXT 
  159.         construction  by the statement  'FOR J=0 TO E0'.   The  parameter 
  160.         E0  or 4000 is used as the upper loop limit to give  the  torpedo 
  161.         plenty  of chance to bounce around the quadrant and be  deflected 
  162.         off  a number of stars.  The  intermediate co-ordinates  (Y1  and 
  163.         X1)  are  then computed by adding the row and column  vectors  to 
  164.         their last value, and these intermediate values are converted  to 
  165.         their  integer  versions (Y2 and X2) in the same  manner  as  the 
  166.         Enterprise was moved across the Quadrant, by the statements
  167.                   'Y1=Y1+Y : X1=X1+X : Y2=INT(Y1) : X2=INT(X1)'.
  168.  
  169.              Line  1060 tests the new co-ordinates of the torpedo to  see 
  170.         if it is outside the sector using the now familiar statement
  171.                     'IF X2<0 OR X2>7 OR Y2<0 OR Y2>7 THEN '.  
  172.         If the torpedo is outside the quadrant, it has missed its  target 
  173.         and a message to that effect is displayed by the statement 
  174.                                'PRINT "MISSED" ', 
  175.         and  the program counter is advanced to line 1210  bypassing  the 
  176.         rest  of  the  subroutine.  If the torpedo is  still  inside  the 
  177.         quadrant, line 1070 displays the co-ordinates using the statement 
  178.         'PRINT  Y2+Z;",";X2+Z' before determining if something  has  been 
  179.         hit.   The  contents  of the sector that  the  torpedo  has  just 
  180.         entered  are  examined by the 'ON' statement in line  1070.   The 
  181.         integer  values  of  the  contents of  the  sector  array  S(I,J) 
  182.         represent  the  contents of the sector where an  empty  or  blank 
  183.         sector is represented by the number one, a two represents a star, 
  184.         if it contains a three, that means that the Enterprise is located 
  185.         in  the sector.  A four identifies the presence of a Klingon  and 
  186.         if  there is a starbase in the sector, its presence is  indicated 
  187.         by  the  number  five.  The 'ON' statement can thus  be  used  to 
  188.         vector  the  program  line to the required line  to  process  the 
  189.         events associated with the torpedo hitting something.
  190.  
  191.  
  192.  
  193.         Copyright (c) Joe Kasser 1989
  194.  
  195.  
  196.  
  197.  
  198.  
  199.         Chapter 8 PAGE 4 STARTREK THE COMPUTER PROGRAM
  200.  
  201.  
  202.              At  line 1080 the torpedo has entered a sector containing  a 
  203.         star.   The  first thing it does is display that fact  using  the 
  204.         statement  'PRINT "STAR "; '.  Notice the space character  inside 
  205.         the quotation marks and the semi-colon after the quotation  marks 
  206.         which inhibits the cursor from advancing to the next line.   When 
  207.         a  torpedo enters a sector containing a star, one of  two  things 
  208.         happen.  The torpedo can hit the star and be absorbed, or it  can 
  209.         pass  close to the star and be absorbed by it.  Which of the  two 
  210.         events is determined purely at random by the statement 
  211.                                  'IF RND(Z)<.5'.  
  212.         In other ways if a random number lying between 0 and 0.99 is less 
  213.         than 0.5 the test passes and the star is deemed to have  absorbed 
  214.         the torpedo.  A message to that effect is then displayed and  the 
  215.         program counter advanced to the last line of the subroutine.   On 
  216.         the  other hand, if the test failed, a message stating  that  the 
  217.         star  has deflected the torpedo is displayed by line 1090,  which 
  218.         then  goes on to set up a random direction and its  vertical  and 
  219.         horizontal vectors before bypassing the remainder of the loop  by 
  220.         advancing the program counter to line 1200 using the statements
  221.             'Y=Z+RND(Z)*8*.785398 : X=COS(Y) : Y=-SIN(Y) : GOTO 1200'
  222.  
  223.              If  the torpedo has entered a sector occupied by a  Klingon, 
  224.         there is a probability that the Klingon can evade the torpedo.  A 
  225.         random  number  is  again  selected using  the  built  in  RND(Z) 
  226.         function.   There is a ten percent probability that  the  Klingon 
  227.         will  evade the torpedo, or a ninety percent probability that  it 
  228.         will  hit  the enemy ship.  Line 1100 takes care of  the  evasion 
  229.         occurrence by using the statement
  230.                             'IF RND(Z)>.9 THEN 1200'.   
  231.         On the other hand if the Klingon was hit, line 1110 displays that 
  232.         event using the statement 
  233.                          'PRINT " KLINGON DESTROYED" '.  
  234.         Having  destroyed  a  Klingon,  the  particular  Klingon  in  the 
  235.         quadrant has to be identified.  This is done by the FOR/NEXT loop 
  236.         of  lines 1120 and 1130.  As there can only be up to eight  enemy 
  237.         ships  in  the quadrant, the loop limits are 0 and 7.   The  loop 
  238.         tests the position co-ordinates of each Klingon (K1(I) AND K2(I)) 
  239.         to  see  if they are identical to those of the  sector  that  the 
  240.         torpedo has entered.  The statement
  241.                       'IF Y2=K1(I) AND X2=K2(I) THEN 1140' 
  242.         does  the job, advancing the program counter out of the  loop  to 
  243.         line 1140 when a match is found.  The loop should only  terminate 
  244.         normally  when  something goes wrong.  An error message  to  that 
  245.         effect  is displayed by the last part of line 1130.  This  should 
  246.         never  happen  when the game is played.  Line  1140  deletes  the 
  247.         Klingon  from the quadrant by setting its energy level  to  zero.  
  248.         The  energy  level of the Klingon K3(I) is used as  a  flag  that 
  249.         states  if  the  Klingon is active or not.  If  the  Klingon  has 
  250.         positive  energy  it  is  alive  and  can  hurt  you.   Once  the 
  251.         energy/active flag has been cleared, the Klingon must be  deleted 
  252.         from  the simulation.   The subroutine starting at line  1220  is 
  253.         then called to perform that operation and the program counter  is 
  254.         advanced to line 1180.
  255.  
  256.              If  the torpedo has entered a sector containing a  starbase, 
  257.  
  258.  
  259.         Copyright (c) Joe Kasser 1989
  260.  
  261.  
  262.  
  263.  
  264.  
  265.         Chapter 8 PAGE 5 STARTREK THE COMPUTER PROGRAM
  266.  
  267.  
  268.         the  program  picks  up at line 1150 by  displaying  a  sarcastic 
  269.         message.  The number of bases in the quadrant (B) and the  number 
  270.         of bases in the galaxy are then decremented, and the values of K8 
  271.         and T9 are then reset to penalize the score by reducing the  size 
  272.         of the multiplier.  The contents of the quadrant are contained in 
  273.         the  quadrant Q(I,J) array.  The starbase is represented  by  the 
  274.         tens digit.  To delete the base from the quadrant, the tens digit 
  275.         must be adjusted.  The sign of the contents of the element in the 
  276.         array  will depend on whether the quadrant has been scanned  into 
  277.         the  computer or not.  Remember, the Enterprise can be  operating 
  278.         in  a quadrant, whose contents are not entered into the  galactic 
  279.         map  because either the computer is down or the relevant  sensors 
  280.         have  been damaged and are not working.  Anyhow, if the  sign  of 
  281.         the  quadrant is negative (unscanned), the a value of ten has  to 
  282.         be added to the value stored in the Q(I,J) array, and if the sign 
  283.         of  the quadrant is positive (scanned) a ten is  subtracted  from 
  284.         the contents of the quadrant.  Line 1160 performs this  operation 
  285.         using the statement
  286.         IF Q(Q1,Q2)<0 THEN Q(Q1,Q2)=Q(Q1,Q2)+10 ELSE Q(Q1,Q2)=Q(Q1,Q2)-10.  
  287.         If the player has destroyed the last base in the game, the  value 
  288.         of  B9  will  be  zero.  Line 1170  detects  that  condition  and 
  289.         displays a nasty message to that effect.
  290.  
  291.              Line  1180 sets the contents of the sector to a blank  using 
  292.         the statement 'S(Y2,X2)=Z' and then advances the program  counter 
  293.         to line 1210.
  294.  
  295.              Meanwhile  if the wandering torpedo has been deflected by  a 
  296.         star  and returns to the sector occupied by the Enterprise,  line 
  297.         1190  displays  a message to that effect.  The  loop  counter  is 
  298.         incremented  in  line 1200 and the next iteration is  allowed  to 
  299.         take  place.   The  subroutine then ends at line  1210.   In  the 
  300.         unlikely event that the J loop ever times out and the loop  exits 
  301.         in  the  normal way, the subroutine will still terminate  in  the 
  302.         normal manner.  If you like you could put a message to the effect 
  303.         that  the  photon torpedo has run out of steam and the  loop  has 
  304.         terminated at the end of line 1200.
  305.  
  306.              The  subroutine that clears the Klingon from the  simulation 
  307.         begins at line 1220 with the usual comment.  A subroutine is used 
  308.         to do the job, because it is done in both the torpedo and  phaser 
  309.         functions  as  well  as  possibly  any  other  functions  to   be 
  310.         identified  later.   Using a subroutine ensures that the  job  is 
  311.         done identically and correctly by the same lines of code whenever 
  312.         the job is to be performed.  Line 1230 begins by decrementing the 
  313.         number of Klingons in both the quadrant (K) and the galaxy  (K9).  
  314.         Remember that the value stored in K9 is the number of Klingons in 
  315.         the  galaxy  multiplied  by a hundred.  If the value  of  K9  has 
  316.         reached  zero, then all the Klingons in the game have been  wiped 
  317.         out and the game can end.  The game-over flag (F9) is then set to 
  318.         five signifying that the player has won the game.  Line 1240 then 
  319.         deletes  the  Klingon from the quadrant array  by  subtracting  a 
  320.         hundred  from the value stored in the quadrant  array  associated 
  321.         with  the  quadrant that the action is taking place.   The  exact 
  322.         mathematical  operation  performed  depends on the  sign  of  the 
  323.  
  324.  
  325.         Copyright (c) Joe Kasser 1989
  326.  
  327.  
  328.  
  329.  
  330.  
  331.         Chapter 8 PAGE 6 STARTREK THE COMPUTER PROGRAM
  332.  
  333.  
  334.         quadrant, namely if it has been scanned into the ship's  computer 
  335.         or not in a similar manner to line 1160 using the statement
  336.         IF Q(Q1,Q2)<0 THEN Q(Q1,Q2)=Q(Q1,Q2)+100 ELSE  Q(Q1,Q2)=Q(Q1,Q2)-100.
  337.         The subroutine ends with the 'RETURN' statement in line 1250.
  338.  
  339.              Copy  lines 1000 to 1250 from figure 7.7 into  your  program 
  340.         and  the newest version.  Debug the program by moving around  the 
  341.         galaxy  to  locate the enemy.  Shoot at everything as  you  move, 
  342.         note how the score goes up as you kill Klingons and then down  as 
  343.         time passes.  Test what happens when you hit Starbases and stars.  
  344.         Is  the  torpedo deflected by stars, what happens when  it  comes 
  345.         back into the sector occupied by the Enterprise?  In other words, 
  346.         test   every  possible  condition  in  which  the  torpedo   hits 
  347.         something,  or  every  branch in the flowchart.   When  you  have 
  348.         finished  and  all  the "bugs" are out of the  program  save  the 
  349.         updated debugged version and read on.
  350.  
  351.         8.2  The PHASERS
  352.  
  353.              Phasers are offensive energy weapons.  They are locked  onto 
  354.         targets automatically, and subject the target to an intense burst 
  355.         of  energy.   They  can  only be  used  to  attack  enemy  ships.  
  356.         Anything  else in the quadrant will not be harmed by the  use  of 
  357.         the Phasers.
  358.  
  359.              The  flow chart for the Phaser function is shown  in  figure 
  360.         8.3.   The  first thing performed is a test for the  presence  of 
  361.         Klingons  in  the  Quadrant.  If there are  no  Klingons  in  the 
  362.         quadrant,  then by definition there is no target in the  quadrant 
  363.         and  a  message  to that effect is displayed.  If  at  least  one 
  364.         target  is present, then the state of repair of the Phaser  Banks 
  365.         is  checked.  If the phasers are not operational they  cannot  be 
  366.         used  and a message to that effect is displayed.  If the  phasers 
  367.         are  usable, then the state of the Short Range sensors has to  be 
  368.         checked,  because  if  they are damaged, the  phasers  cannot  be 
  369.         locked  on to the target and Mr. Sulu will have to use  a  manual 
  370.         'best  guess'  technique which of course is not  as  accurate  as 
  371.         using the computer.  In that condition a message is displayed  to 
  372.         that effect.
  373.  
  374.              Energy is next allocated to the phaser banks by the  player.  
  375.         Phaser control then equally divides that amount of energy by  the 
  376.         number  of  active targets in the quadrant and fires a  burst  at 
  377.         each  Klingon.  The amount of energy hitting the enemy's  shields 
  378.         is  computed.  The hit is deducted from the shield energy of  the 
  379.         Klingon,  and  a message is displayed stating that a  'hit'  took 
  380.         place.   If the shields were beaten down to less than  zero,  the 
  381.         Klingon  has been destroyed and is deleted from the quadrant  and 
  382.         the game status parameters are updated.  If the shields were just 
  383.         beaten  down to a low value, the Klingon captain may decide  that 
  384.         his position is hopeless and self-destruct or even surrender.  If 
  385.         the  Klingon  self-destructs,  it must be  boarded  before  being 
  386.         deleted  from  the game.  If it self-destructs it  must  also  be 
  387.         deleted from the game.  A bonus score is awarded in either  case, 
  388.         to  compensate the player for being damaged by the Klingon  while 
  389.  
  390.  
  391.         Copyright (c) Joe Kasser 1989
  392.  
  393.  
  394.  
  395.  
  396.  
  397.         Chapter 8 PAGE 7 STARTREK THE COMPUTER PROGRAM
  398.  
  399.  
  400.         trying  to get it to surrender or self-destruct.  If the  Klingon 
  401.         can still fight on after the hit, the loop finds the next  active 
  402.         Klingon target in the Quadrant and tries again.
  403.  
  404.              The BASIC language implementation of the flowchart can  take 
  405.         the  form shown in figure 8.4.  It begins with the  usual  REMark 
  406.         statement  in  line  800.  Line 810 then  checks  the  number  of 
  407.         Klingons  in  the  quadrant.  If  there  are  none  present,   by 
  408.         definition  there  are  no targets and  a  message  is  displayed 
  409.         accordingly.   Line  820  then  tests the  state  repair  of  the 
  410.         Phasers.   If  they are damaged, represented by  a  positive  ETR 
  411.         value  in  the Damage Control / Command array 'D(I)',  a  message 
  412.         stating  that  they  are "OUT OF ACTION " is  displayed  and  the 
  413.         remainder  of  the  subroutine  is bypassed  by  the  'GOTO  950' 
  414.         statement  which advanced the program line counter to  line  950.  
  415.         If  the  Short Range Sensors are not working, then  the  computer 
  416.         cannot  lock  the  phasers  onto target so  the  program  has  to 
  417.         determine if they are operational.  Line 830 thus looks at  their 
  418.         state  of  repair, represented by the '1' element in  the  Damage 
  419.         Control / Command Array in the usual way.  If they are damaged, a 
  420.         message to that effect is displayed, and Mr. Sulu announces  that 
  421.         he will use his best guess to locate the targets.  The  statement 
  422.         used  to  display the message combines two strings  in  the  same 
  423.         "PRINT' statement as follows
  424.          'PRINT D$(1);"are not working, will use best guess settings" '.
  425.         The D$(I) string array contains the names of all the Commands  or 
  426.         sub-systems.  The program has been laid out so that they are  the 
  427.         same.  D$(1) is thus 'SHORT RANGE SENSORS'.  The semi colon  then 
  428.         ensures  that  the  cursor stays in place  after  the  string  is 
  429.         displayed,  and the rest of the message contained in  the  string 
  430.         constant is then displayed.
  431.  
  432.              The  dialogue to allow the player to tell the  computer  how 
  433.         much  energy to allocate to the phasers begins in line  840.  The 
  434.         statements
  435.         'INPUT "READY- how much energy do you want to fire "; X : IF X<=0 THEN 950'
  436.         prompt  the player for an input while indicating what exactly  is 
  437.         to  be  input.  The input is assigned to the  temporary  variable 
  438.         'X', and then checked to ensure that is has a positive value.  If 
  439.         it  does  not  have  a positive value,  the  program  counter  is 
  440.         advanced  to  line  950.  The player can thus  enter  a  zero  in 
  441.         response to the prompt to bypass the phaser firing operation.
  442.  
  443.              Line 850 then makes sure that the ship has enough energy  on 
  444.         board  to  allow the phasers to fire it.  There is  no  point  in 
  445.         allocating  500  units  of  energy to the  phaser  banks  if  the 
  446.         Enterprise  only has 400 units at that particular time.   If  the 
  447.         player tries it on, line 850 catches it and displays a message to 
  448.         that  effect.   The  last statement on  the  line  redirects  the 
  449.         program counter to the prompt statement in line 840 so the player 
  450.         can  enter  a  correct value.  It is possible  at  this  time  to 
  451.         display the actual amount of energy (E) remaining on the ship  to 
  452.         tell the player, but this version does not so as to penalize  the 
  453.         player.  The test for available energy is 'IF X>(E-E1) '  because 
  454.         the  amount  of energy to be fired must be less  than  the  total 
  455.  
  456.  
  457.         Copyright (c) Joe Kasser 1989
  458.  
  459.  
  460.  
  461.  
  462.  
  463.         Chapter 8 PAGE 8 STARTREK THE COMPUTER PROGRAM
  464.  
  465.  
  466.         amount  of  ship's  energy excluding the amount  in  the  Shields 
  467.         (represented  by  the variable 'E1').  Line 860   starts  off  by 
  468.         subtracting  the  amount of energy to be fired  from  the  ship's 
  469.         power.     The number of Klingons is then stored in  a  temporary 
  470.         variable  (Y).  This is done because when firing the  phasers  in 
  471.         line  880, the amount of energy that was allocated is divided  by 
  472.         the number of Klingons in the quadrant.  If line 880 used 'K'  to 
  473.         represent  the  number  of Klingons, what  happens  when  one  is 
  474.         destroyed  in  the  middle  of a round of  fire  ?   It  is  thus 
  475.         mandatory  to  use  a temporary variable to keep  the  number  of 
  476.         Klingons the same at the end of the round as at the beginning, at 
  477.         least   as  far  as  the  firing  sequence  is  concerned.    The 
  478.         decrementing  of  the  number  of Klingons  in  the  quadrant  is 
  479.         performed  in the subroutine beginning in line 1220 in  case  you 
  480.         are  searching  for it in the listing in figure 8.4  and  haven't 
  481.         found  it.   A  loop is then begun to locate and  shoot  at  each 
  482.         Klingon in the Quadrant.  The 'FOR/NEXT' construct is used.   The 
  483.         limit  on  the  loop are 0 and 7 because there can  only  be,  by 
  484.         definition,  eight  enemy  ships  in  the  quadrant,  and   their 
  485.         parameters are stored in the arrays K1(I), K2(I) and K3(I).   The 
  486.         last  statement on the line tests the amount of energy  allocated  
  487.         to the Klingon.  If it is positive, the Klingon is active and the 
  488.         test  fails, the program flow continuing on the  next  sequential 
  489.         line.   If  the Klingon has negative or zero energy,  it  is  not 
  490.         active  and the program counter is branched forward  directly  to 
  491.         the NEXT statement of line 940.   Line 870 then samples the state 
  492.         of  repair  of the Short Range Sensors represented  by  the  1'th 
  493.         element in the Damage Control / Command Array D(I).  Note this is 
  494.         really the 1'th element, not the first element, because the first 
  495.         element  is  D(0)  which represents the state of  repair  of  the 
  496.         engines.   If the Short Range Sensors are damaged, Mr. Sulu  will 
  497.         have  to  use  his  best guess.  The  statement  'X  =  X*RND(Z)' 
  498.         performs  that  operation.  A random number is generated  by  the 
  499.         random  number  generator  built into BASIC  using  the  'RND(1)' 
  500.         function.  The result is then multiplied by 'X' to give a  random 
  501.         percentage of the original value of 'X'.  This random  percentage 
  502.         is  then  stored  back  in  'X' and is  used  as  the  basis  for 
  503.         determining  the  amount of the hit.  Mr. Sulu's best  guess  has 
  504.         thus  been  implemented using the random number  generator.   The 
  505.         actual amount of energy that hits each enemy is computed in  line 
  506.         880 by the statement
  507.                  'H = X/(Y*(SQR((K1(I)-S1)^2+(K2(I)-S2)^2)^.4))' 
  508.         which  divides the amount of energy to be fired by the number  of 
  509.         Klingons in the quadrant (Y) multiplied by a factor  representing 
  510.         the  distance between the Enterprise and the particular  Klingon.  
  511.         The distance between the Enterprise and the Klingon is calculated 
  512.         using  Pythagoras's  theorem.   The last statement  of  the  line 
  513.         subtracts the amount in the hit from the amount of energy in  the 
  514.         Klingon.  The array variables K1(I) contains the row co-ordinates 
  515.         of  the  Klingon,  corresponding to the S1  co-ordinates  of  the 
  516.         Enterprise.   K2(I)  contains the column co-ordinates  and  K3(I) 
  517.         contains the energy quotient of the Klingon.
  518.  
  519.              The  first  part of line 890 displays a  message  announcing 
  520.         that a hit was achieved on a Klingon and adds the co-ordinates of 
  521.  
  522.  
  523.         Copyright (c) Joe Kasser 1989
  524.  
  525.  
  526.  
  527.  
  528.  
  529.         Chapter 8 PAGE 9 STARTREK THE COMPUTER PROGRAM
  530.  
  531.  
  532.         the  Klingon.  Since the computer stores the co-ordinates in  the 
  533.         range  0  to 7, while the player reads them as from 1 to  8,  the 
  534.         conversion   is   contained  in  the  PRINT  statement   by   the 
  535.         'K1(I)+Z;",";K2(I)+Z;' which also formats the display.  The  last 
  536.         part  of  the  line displays the amount of  energy  left  in  the 
  537.         Klingon  if the Short Range Sensors are working.  The  format  of 
  538.         the statement used is
  539.             'IF D(1)=0 THEN PRINT "(";K3(I);"Units left)" ELSE PRINT' 
  540.         because, if the sensors are damaged, the PRINT following the ELSE 
  541.         advances  the  cursor  to the next line  without  displaying  any 
  542.         message.   Other versions of the game indicate the amount of  the 
  543.         hit on the Klingon.  This can be achieved by adding 'H;"Unit'  in 
  544.         front  of the word 'Hit' on line 890.  If you do that,  then  the 
  545.         amount  of  the hit will be displayed whatever the state  of  the 
  546.         Short  Range Sensors.  That is not "realistic", thus if you  want 
  547.         the amount of the hit displayed, you may change line 890 to  take 
  548.         the state of those sensors into account.
  549.  
  550.              Line  900 first tests to see if the Klingon  was  destroyed.  
  551.         It  detects that the Klingon was destroyed by checking to see  if 
  552.         it  has  any  energy  left.  If it does not  have  any,  then  by 
  553.         definition it has been destroyed and the program continues  along 
  554.         the  line.  If the test failed, the program counter  advances  to 
  555.         the next line number in sequence.  In the event that the  Klingon 
  556.         was  destroyed,  a message stating that fact is  displayed.   The 
  557.         contents  of the element in the sector array that  contained  the 
  558.         Klingon are then rest to reflect a blank sector by the statement
  559.                               'S(K1(I),K2(I)) = Z'.
  560.         The  subroutine  starting at line 1220 is invoked  to  completely 
  561.         clear  the  Klingon  from the game and  the  program  counter  is 
  562.         advanced  to  line 940 to increment the loop  counter.    If  the 
  563.         Klingon was not destroyed the program continues at line 910 which 
  564.         decides  if the Klingon is going to self destruct  or  surrender.  
  565.         The  line thus starts with a test to determine if the Klingon  is 
  566.         alone in the quadrant (IF K>Z) or if the Klingon has only a small 
  567.         amount  of energy left (K3(I)>E1/100).  The test to determine  of 
  568.         the Klingon is alone is a simple test to see if the value of  'K' 
  569.         is greater than 1.  If it is, then obviously there are more  than 
  570.         one  Klingons in the quadrant, and the target is not alone.   The 
  571.         energy  test compares the amount of energy that the  Klingon  has 
  572.         with  the amount of energy in the Shields of the Enterprise.   If 
  573.         the  Klingon has more that a hundredth fraction of the energy  in 
  574.         the shields of the Enterprise, it is deemed to have sufficient to 
  575.         fight  on.   The  last  part  of the  test  also  adds  a  random 
  576.         probability  of  sixty percent that the Klingon  will  choose  to 
  577.         fight  on  even if the other conditions are met.  Note  that  the 
  578.         test  is phrased 'RND(Z)>.4' which means that the test  fails  if 
  579.         the  probability is less than forty percent and the test in  this 
  580.         line  passes  if the Klingon is to fight on.  The  random  number 
  581.         generator  is used here to determine the probability of an  event 
  582.         occurring.   The random number generator returns a random  number 
  583.         between 0 and 0.99. Comparing that number with a preset threshold 
  584.         level  can thus provide a probability level saying that an  event 
  585.         will  only  happen  if the number is greater or  less  than  some 
  586.         value.   In  this case, the Klingon fights on if any one  of  the 
  587.  
  588.  
  589.         Copyright (c) Joe Kasser 1989
  590.  
  591.  
  592.  
  593.  
  594.  
  595.         Chapter 8 PAGE 10 STARTREK THE COMPUTER PROGRAM
  596.  
  597.  
  598.         tests  in  line 910 pass and the program counter is  advanced  to 
  599.         line  940  bypassing the surrender/self destruct section  of  the 
  600.         subroutine.
  601.  
  602.              The  Klingon  has to be removed from the game in  the  event 
  603.         that  it  self destructs or fights on.  Line  920  performs  that 
  604.         operation.  the first statement on the line clears the  Klingon's 
  605.         energy.   The  second  clears the Klingon from  the  quadrant  by 
  606.         setting  the  co-ordinates of the sector it is in  to  the  blank 
  607.         state.   The subroutine starting at line 1220 is then invoked  to 
  608.         clear  the  Klingon from the game.  The line  then  continues  by 
  609.         using the random number generator to determine if the Klingon has 
  610.         surrendered  or  destroyed itself.  There is  a  seventy  percent 
  611.         probability  that the Klingon captain will decide to destroy  his 
  612.         ship rather than be captured.  Klingons as a rule don't surrender 
  613.         and rarely take prisoners.  The probability of them  surrendering 
  614.         is thus small.  It happens from time to time because the  captain 
  615.         and the officers may be killed, or the self destruct mechanism or 
  616.         other parts of the ship may be damaged or for any one of a number 
  617.         of  reasons.  In any event, this simulation sets the  probability 
  618.         of  surrender  at about thirty percent.  If the Klingon  did  not 
  619.         surrender, the last part of line 920 is performed.  A message  is 
  620.         displayed stating that the Klingon exploded.  There is no need to 
  621.         test  the state of the Short Range Sensors at this  time  because 
  622.         the  explosion  of a Klingon ship is so powerful that it  can  be 
  623.         detected by visual means alone.  The number of Klingons that have 
  624.         committed suicide represented by the value stored in the variable 
  625.         'K5'  is incremented and the program counter is skipped  forwards 
  626.         to  line  940.  The variable 'K5' is used to  provide  a  reduced 
  627.         bonus  on the player's score because the player  usually  suffers 
  628.         damage  trying  to  get the Klingon to surrender  and  should  be 
  629.         compensated  accordingly.   It  is much easier to  wipe  out  the 
  630.         Klingon completely than to try and make it surrender.
  631.  
  632.              In the event that the Klingon surrenders line 930 displays a 
  633.         message  to  that  effect.   The number  of  Klingons  that  have 
  634.         surrendered represented by the variable 'K4' is incremented.   If 
  635.         a  Klingon  surrenders it provides a multiplied on  the  player's 
  636.         score.  A time penalty is thus inserted at the moment of capture.  
  637.         It takes half a stardate to board the enemy, secure the prisoners 
  638.         survey the ship, transfer a prize crew and take care of any other 
  639.         outstanding  matters.  During this time however,  Damage  control 
  640.         have not been idle and have been busily working away on any  sub-
  641.         systems damaged during the battle.  Line 930 thus first sets  the 
  642.         temporary  time variable 'T1' to the value of the time  'T'.   It 
  643.         then  subtracts  half  a stardate from that time  and  calls  the 
  644.         subroutine starting at line 1800 to update the state of repair of 
  645.         the ship after the passage of half a stardate.  
  646.  
  647.              Line 940 reiterates the loop counter and line 950 terminates 
  648.         the function.  
  649.  
  650.              Carefully copy lines 800 to 950 to your program and save the 
  651.         latest version.  Debug them by using the Phasers in all  possible 
  652.         conditions.  Try firing them with no enemy ships in the quadrant.  
  653.  
  654.  
  655.         Copyright (c) Joe Kasser 1989
  656.  
  657.  
  658.  
  659.  
  660.  
  661.         Chapter 8 PAGE 11 STARTREK THE COMPUTER PROGRAM
  662.  
  663.  
  664.         Try  to  get the Klingon to surrender or self-destruct.   If  you 
  665.         have a problem getting an event to occur, change the  probability 
  666.         temporarily  so  that  you can debug it  properly.   When  it  is 
  667.         completely debugged save it again and read on.
  668.  
  669.         8.3  The SHIELDS Command
  670.  
  671.              The  Shields command allows the player to set up the  amount 
  672.         of energy in the Shields of the Enterprise.  The shields  protect 
  673.         the  Enterprise against the effects of Klingon phaser  blasts  as 
  674.         well  as  Klingon  Space Mine explosions.   If  the  shields  are 
  675.         damaged,  they will function normally but the player will not  be 
  676.         able to change the amount of energy allocated to the shields.
  677.  
  678.              The flow chart for the SHIELDS function is straight  forward 
  679.         and  is shown in figure 8.5.  It begins with a test to  determine 
  680.         if  the  shields  are damaged.  If they are, a  message  to  that 
  681.         effect is displayed and the routine terminates.  If they are not, 
  682.         some dialogue takes place to determine the amount of energy to be 
  683.         allocated  to the shields.  The player can lower the  shields  by 
  684.         allocating zero units or can raise the shields to the full amount 
  685.         of energy carried by the Enterprise.  The usual value set in  the 
  686.         shields at any one time is about 400 units.  If the player  tries 
  687.         to allocate more energy than the Enterprise has, an error message 
  688.         will  be  displayed.   Once a valid amount  of  energy  has  been 
  689.         allocated, it is put into the shields, and the sequence ends.
  690.  
  691.              The  BASIC language implementation of the  Shields  function 
  692.         could be as shown in figure 8.6 with the usual REMark  statement.  
  693.         Line 2710 begins the action by testing the state of the  'docked' 
  694.         flag  (C$).   If  the Enterprise is docked,  the  player  is  not 
  695.         allowed  to  raise the shields by definition.   The  shields  are 
  696.         lowered  automatically by the docking process.  there is thus  no 
  697.         further need to raise the shields while the Enterprise is  docked 
  698.         at  a starbase.  Operationally this means that  when  un-docking, 
  699.         the player must move away from the base using impulse power (at a 
  700.         speed  of less than Warp One), then raise the shields by  putting 
  701.         some energy into them before changing to warp speed to depart for 
  702.         distant  quadrants. The last statement on the line  advances  the 
  703.         program  line  counter to the end of the subroutine.   Line  2720 
  704.         tests  the  state of the shield element in the Damage  Control  / 
  705.         Command  Array  (D(I)).  If they are damaged, a message  to  that 
  706.         effect is displayed by the 
  707.                               PRINT D$(I)+"DAMAGED" 
  708.         statement before the program counter is advanced past the rest of 
  709.         the subroutine by the ' GOTO 2770' statement.  If the Shields are 
  710.         fully  operational, line 2730 displays that fact by generating  a 
  711.         message  notifying the player as to how much energy is  allocated 
  712.         to  the shields at that particular time.  Notice that  line  2720 
  713.         uses  a  plus sign to separate the two elements  to  be  printed, 
  714.         while  line  2730 uses a semi-colon.  Line  2730  first  displays 
  715.         D$(I) which in this instance is "SHIELDS " and then displays  the 
  716.         following  items.  There are four things being displayed  in  the 
  717.         line.   The contents of the element in the Command  String  Array 
  718.         D$(I),  the  string constant, the contents of the  variable  'E1' 
  719.  
  720.  
  721.         Copyright (c) Joe Kasser 1989
  722.  
  723.  
  724.  
  725.  
  726.  
  727.         Chapter 8 PAGE 12 STARTREK THE COMPUTER PROGRAM
  728.  
  729.  
  730.         which  contains the amount of energy in the shields, and  lastly, 
  731.         the  string  constant  "Units".   Line 2720  on  the  other  hand 
  732.         contained the statement 'PRINT D$(I)+"DAMAGED" ' which added  two 
  733.         separate strings together at the same time as they were  printed.  
  734.         The  statement  could just as well have been  written  as  
  735.                             'PRINT D$(I);"DAMAGED" '.
  736.  
  737.              The dialogue to allocate the energy to the shields begins in 
  738.         line  2740.  Again the 'INPUT' statement is used together with  a 
  739.         prompt message telling the player what is expected as the  reply.  
  740.         The  number  of energy units to be allocated to  the  shields  is 
  741.         temporarily  stored  in  the  variable  'N'  until  it  has  been 
  742.         validated.  The first validation test is performed at the end  of 
  743.         line 2740.  It ensures that the amount of energy to be  allocated 
  744.         to  the Shields is not a negative value.  If the player  input  a 
  745.         negative  value  the  last item on line  2470  ensures  that  the 
  746.         question  is  asked  once  again.  this  technique  is  known  as 
  747.         'trapping'.   The negative response is trapped at this  time  and 
  748.         blocked  here before it can cause trouble later.  An  alternative 
  749.         action would have been to state
  750.                                 'IF N<0 THEN N=0' 
  751.         which  would  also trap negative values but in a  different  way.  
  752.         The alternative ensures that all negative values are converted to 
  753.         zero values.  The method used here inhibits negative values,  and 
  754.         uses  the  zero  value  to signify that  the  shields  have  been 
  755.         lowered.
  756.  
  757.              The second validation test is on line 2750.  It ensures that 
  758.         there  is  enough  energy in the dilithium  crystals  aboard  the 
  759.         Enterprise to allocate the desired amount to the shields.  If the 
  760.         amount  of  energy on the starship is less than  the  amount  the 
  761.         player wants to allocate to the shields, then a nasty message  to 
  762.         that  effect is displayed and the question is asked again.   Once 
  763.         the  entry has been validated, the contents of the  variable  'N' 
  764.         are  copied into the variable 'E1' which represents the  contents 
  765.         of  the shields by the statement on line 2760 and the  subroutine 
  766.         terminates at line 2770.  If the temporary variable was not  used 
  767.         to store the desired allocation of shield power before validation 
  768.         the previous amount of energy in the shields would have been lost 
  769.         when  the  erroneous entry was input.  In this  application  that 
  770.         would  not have mattered, because the routine is not  allowed  to 
  771.         terminate  until  a valid amount of energy is  allocated  to  the 
  772.         shields.  In other cases however, that construction may not  hold 
  773.         true and the use of a temporary variable is mandatory.
  774.  
  775.              Copy  the lines listed in figure 8.6 into your  program  and 
  776.         debug them by trying the Shields (SHE) command.  Try to  allocate 
  777.         more  energy  than  the Enterprise has.  Try  less,   Damage  the 
  778.         shields  and try again.  Test all possibilities.   When  you  are 
  779.         satisfied that the shields are operational, save the program  and 
  780.         read on.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.         Copyright (c) Joe Kasser 1989
  788.  
  789.  
  790.  
  791.  
  792.  
  793.